package com.jindy.module.outboundorder.controller;

import com.jindy.common.annotation.Log;
import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.core.domain.entity.SysUser;
import com.jindy.common.core.page.TableDataInfo;
import com.jindy.common.enums.BusinessType;
import com.jindy.common.utils.SecurityUtils;
import com.jindy.common.utils.poi.ExcelUtil;
import com.jindy.module.order.domain.Order;
import com.jindy.module.outboundorder.domain.OutboundOrder;
import com.jindy.module.outboundorder.service.DepositWarningService;
import com.jindy.module.outboundorder.service.IOutboundOrderService;
import com.jindy.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 出库单Controller
 *
 * @author ruoyi
 * @date 2022-10-18
 */
@RestController
@RequestMapping("/module/outboundOrder")
public class OutboundOrderController extends BaseController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(OutboundOrderController.class);
    @Autowired
    private IOutboundOrderService outboundOrderService;

    @Resource
    private DepositWarningService depositWarningService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询出库单列表
     */
    @PreAuthorize("@ss.hasPermi('module:outboundOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(OutboundOrder outboundOrder)
    {
//        //添加权限
//        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
//        outboundOrder.setUserId(sysUser.getUserId());
        //添加权限
//        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
//        SysDept dept =sysUser.getDept();
//        if(dept.getDeptName().indexOf("市场")>=0){
//            outboundOrder.setUserId(sysUser.getUserId());
//        }

        startPage();
        List<OutboundOrder> list = outboundOrderService.selectOutboundOrderList(outboundOrder);
        return getDataTable(list);
    }

    /**
     * 输入父单id查询出库单列表(子出库单）和子单机器列表
     *
     * @param outboundOrder 出库单
     * @return 出库单
     */
//    @PreAuthorize("@ss.hasPermi('module:outboundOrder:list3')")
    @GetMapping("/list3")
    public TableDataInfo list3(OutboundOrder outboundOrder)
    {
        startPage();
        List<OutboundOrder> list = outboundOrderService.selectOutboundOrderList3(outboundOrder);
        return getDataTable(list);
    }

    /**
     * 查询未填物流费用的出库子单列表
     */
    @PreAuthorize("@ss.hasPermi('module:outboundOrder:list2')")
    @GetMapping("/list2")
    public TableDataInfo list2(OutboundOrder outboundOrder)
    {
        //        //添加权限
        //        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        //        outboundOrder.setUserId(sysUser.getUserId());
        //添加权限
        //        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        //        SysDept dept =sysUser.getDept();
        //        if(dept.getDeptName().indexOf("市场")>=0){
        //            outboundOrder.setUserId(sysUser.getUserId());
        //        }

        startPage();
        List<OutboundOrder> list2 = new ArrayList<>();
        List<OutboundOrder> outboundOrderList = outboundOrderService.selectOutboundOrderList2(outboundOrder);
        outboundOrderList.forEach(outbound->{
            if(!outbound.getParentId().equals(0L) && outbound.getLogisticsExpenses() == null){
                list2.add(outbound);
            }
        });
        return getDataTable(list2);
    }

    /**
     * 导出出库单列表
     */
    @PreAuthorize("@ss.hasPermi('module:outboundOrder:export')")
    @Log(title = "出库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OutboundOrder outboundOrder)
    {
//        //添加权限
//        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
//        outboundOrder.setUserId(sysUser.getUserId());

        List<OutboundOrder> list = outboundOrderService.selectOutboundOrderList(outboundOrder);
        ExcelUtil<OutboundOrder> util = new ExcelUtil<OutboundOrder>(OutboundOrder.class);
        util.exportExcel(response, list, "出库单数据");
    }

    /**
     * 获取出库单详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:outboundOrder:id')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(outboundOrderService.selectOutboundOrderById(id));
    }

    /**
     * 新增出库单
     */
//    @PreAuthorize("@ss.hasPermi('module:outboundOrder:add')")
    @Log(title = "出库单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OutboundOrder outboundOrder)
    {
        try {
            return toAjax(outboundOrderService.insertOutboundOrder(outboundOrder));
        } catch (Exception e) {
            LOGGER.error("新增出库单失败：",e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改出库单
     */
    @PreAuthorize("@ss.hasPermi('module:outboundOrder:update')")
    @Log(title = "出库单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OutboundOrder outboundOrder)
    {
        return toAjax(outboundOrderService.updateOutboundOrder(outboundOrder));
    }


    /**
     * 修改出库单物流信息
     */
    @PreAuthorize("@ss.hasPermi('module:outboundOrder:edit')")
    @Log(title = "修改出库单物流信息", businessType = BusinessType.UPDATE)
    @PostMapping("/editLogistics")
    public AjaxResult editLogistics(@RequestBody OutboundOrder outboundOrder)
    {
        return toAjax(outboundOrderService.updateOutboundOrder(outboundOrder));
    }
    /**
     * 获取出库单编号
     */
    @Log(title = "修改出库单物流信息", businessType = BusinessType.UPDATE)
//    @PreAuthorize("@ss.hasPermi('module:outboundOrder:getNo')")
    @PostMapping("/getNo")
    public AjaxResult getNo(@RequestBody OutboundOrder outboundOrder)
    {
        return AjaxResult.success(outboundOrderService.getNo(outboundOrder));
    }

    /**
     * 删除出库单
     */
    @Log(title = "出库单", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('module:outboundOrder:delete')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(outboundOrderService.deleteOutboundOrderByIds(ids));
    }

    /**
     * 查询出库单押金预警列表
     */
    @PreAuthorize("@ss.hasPermi('module:outboundOrder:warning')")
    @GetMapping("/warning")
    public TableDataInfo Warning(Order order){

        //添加权限
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        order.setUserId(sysUser.getUserId());
        startPage();
        List<Order> list = depositWarningService.selectWarningOrderList(order);
        return getDataTable(list);
    }


}
